/**
* Tools services
*/
app.factory("toolsServices", function($http) {	
	console.info("start to register toolsServices");
	var translations = undefined;
	var parameters = undefined;
	var currentUser = undefined;
	return {
		setParameters: function(json){
			parameters = json;
		},
		getParameters: function(){
			return parameters;
		},
		getConfiguration: function(callback) {
			console.info("toolsServices - get configuration called"); 
			$http.post(app.servicebase, { "service" : "getConfiguration"}).
			success(function(data, status) 
			{
				console.info("getConfiguration - call success"); 
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("getConfiguration - call failed"); 	
				throw status + ' : ' + data;	
			});
		},
		getConfigurationFiles: function(callback) {
			console.info("toolsServices - getConfigurationFiles called"); 
			$http.post(app.servicebase, { "service" : "getParametersFiles"}).
			success(function(data, status) 
			{
				console.info("getParametersFiles - call success"); 
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("getConfigurationFiles - call failed"); 	
				throw status + ' : ' + data;	
			});
		},
		setCurrentUser: function(data){
			console.info("setCurrentUser - called with data : " + data); 
			if(typeof(Storage) != undefined) 
			{
				localStorage.setItem("CurrentUser", JSON.stringify(data));
			} 
			else
			{
				$cookieStore.put("CurrentUser", JSON.stringify(data));
			}
			currentUser = data;
		},
		getCurrentUser: function(){
			console.info("getCurrentUser - called"); 
			if(typeof(Storage) != undefined) {
				if (localStorage.getItem("CurrentUser") != undefined) {
					currentUser = JSON.parse(localStorage.getItem("CurrentUser"));
				}
			} 
			else 
			{
				currentUser = JSON.parse($cookieStore.get('CurrentUser'));
			}
			return currentUser;
		},
		removeCurrentUser: function(){
			console.info("removeCurrentUser - called"); 
			if(typeof(Storage) != undefined) 
			{
				currentUser = localStorage.removeItem("CurrentUser");
			} 
			else 
			{
				currentUser = $cookieStore.remove('CurrentUser');
			}
			currentUser = undefined;
		},
		getTranslations: function(callback) {
			console.log('toolsServices.getTranslations - get all translations');
			$http.post(app.servicebase, { "service" : "getTranslations" }).
			success(function(data, status) 
			{
				console.info("getTranslations - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				translations = data.response;
				callback(translations);
			})
			.
			error(function(data, status) 
			{
				console.error("toolsServices.getTranslations - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
	};
});